﻿/*********************************************************************************************************************
Unit tests VanguardDocumentation, Procedure SetBaseEntity

© 2014 Erik Ackerholm

This file is part of SqlVanguard.

SqlVanguard is free software: you can redistribute it and/or modify it under the terms of the GNU General Public 
License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later 
version.

SqlVanguard is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with SqlVanguard. If not, see 
<http://www.gnu.org/licenses/>.
*********************************************************************************************************************/

set nocount on;
set xact_abort off;
GO

create table #TestCases (
	TestNo              integer not null           identity (1, 1),
	InputEntityName     nvarchar(512),
	OutputEntityName    nvarchar(512),
	ErrorCode           integer,
	primary key (TestNo)
);
GO

insert into #TestCases
values
	('TestSchema', 'TestSchema', null),
	('TestSchema.TestTable', 'TestSchema.TestTable', null),
	('Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 ', 'Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 Name of extreme length 12312312312312312312312312 ', null),
	('', null, null),
	('', null, null),
	(null, null, null),
	('Test', 'Test', null),
	('    ', null, null);
GO

declare @TestNo as integer;
declare @InputEntityName as nvarchar(512);
declare @OutputEntityName as nvarchar(512);
declare @ErrorCode as integer;

set @TestNo = (select min(TestNo) from #TestCases);

while
	@TestNo is not null
begin
	
	-- Fetch input data
	set @InputEntityName = (select InputEntityName from #TestCases where TestNo = @TestNo);
	
	-- Reset output variables
	set @OutputEntityName = null;
	
	begin try;
		execute VanguardDocumentation.SetBaseEntity @InputEntityName;
		set @OutputEntityName = VanguardDocumentation.GetBaseEntity();
	end try
	begin catch
		set @ErrorCode = error_number();
	end catch;

	-- No error and difference in output
	if
		exists (
			select TestNo, InputEntityName, OutputEntityName
			from #TestCases
			where TestNo = @TestNo and ErrorCode is null
			except
			select @TestNo, @InputEntityName, @OutputEntityName
			where @ErrorCode is null
		)
	begin
		print 'SetBaseEntity, Failed test no ' + rtrim(@TestNo);
		print '	' + coalesce(quotename(@InputEntityName, ''''), 'null')
			+ ' >>> ' + coalesce(quotename(@OutputEntityName, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end
	-- Difference in errors (ignores differences in output for errors)
	else if
		exists (
			select TestNo, InputEntityName, ErrorCode
			from #TestCases where TestNo = @TestNo
			except
			select @TestNo, @InputEntityName, @ErrorCode
		)
	begin
		print 'SetBaseEntity, Failed test no ' + rtrim(@TestNo);
		print '	' + coalesce(quotename(@InputEntityName, ''''), 'null')
			+ ' >>> ' + coalesce(quotename(@OutputEntityName, ''''), 'null')
			+ ' (Error ' + coalesce(rtrim(@ErrorCode), 'null') + ')';
	end;
	
	set @TestNo = (select min(TestNo) from #TestCases where TestNo > @TestNo);

end;
GO

drop table #TestCases;
GO

set nocount off;
GO

-- Checks permissions
execute as user = 'WriterA';

declare @BaseEntity as nvarchar(512);

begin try;
	execute VanguardDocumentation.SetBaseEntity 'TestSchema';
	set @BaseEntity = VanguardDocumentation.GetBaseEntity();
end try
begin catch;
	raiserror ('Failed permissions of SetBaseEntity/GetBaseEntity for VanguardDocumentationWriter', 16, 1);
end catch;

revert;
GO

execute as user = 'ReaderB';

declare @BaseEntity as nvarchar(512);

begin try;
	execute VanguardDocumentation.SetBaseEntity 'TestSchema';
	set @BaseEntity = VanguardDocumentation.GetBaseEntity();
end try
begin catch;
	raiserror ('Failed permissions of SetBaseEntity/GetBaseEntity for VanguardDocumentationReader', 16, 2);
end catch;

revert;
GO

execute as user = 'NormalUserC';

declare @BaseEntity as nvarchar(512);

begin try;
	execute VanguardDocumentation.SetBaseEntity 'TestSchema';
	raiserror ('Failed permissions of SetBaseEntity for unprivileged user', 16, 3);
end try
begin catch;
	set @BaseEntity = null;
end catch;

begin try;
	set @BaseEntity = VanguardDocumentation.GetBaseEntity();
	raiserror ('Failed permissions of GetBaseEntity for unprivileged user', 16, 4);
end try
begin catch;
	set @BaseEntity = null;
end catch;

revert;
GO




